package com.sgj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sgj.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Guoji Shen
 * @Date: 2021/5/19 16:18
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("[OrderHystrixController][paymentInfo_OK] result:{}", result);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
    // @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutFallbackMethod", commandProperties = {
    //         // 设置峰值为 3s，超过 3s 则会调用 paymentInfo_TimeOutFallbackMethod() 方法
    //         @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    // })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("[OrderHystrixController][paymentInfo_TimeOut] result:{}", result);
        return result;
    }

    public String paymentInfo_TimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者 80，对方支付系统繁忙，请 10 秒钟后再试或调用方法本身出错，请认真检查一下";
    }

    // 下面是全局 fallback 方法
    public String payment_Global_FallbackMethod() {
        return "Global 异常处理信息，请稍后再试！";
    }

}
